package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.DemandEntity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.DemandActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemLvDemanActivityAdapter extends BaseAdapter {

    private List<DemandEntity.ListEntity> mList = new ArrayList<>();

    private  Context context;
    private LayoutInflater layoutInflater;

    public ItemLvDemanActivityAdapter(Context context, List<DemandEntity.ListEntity> list) {
        this.context = context;
        this.mList=list;
        this.layoutInflater = LayoutInflater.from(context);
    }
    public void setList(List<DemandEntity.ListEntity> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public DemandEntity.ListEntity getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_deman_activity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((DemandEntity.ListEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(final DemandEntity.ListEntity object, ViewHolder holder) {
        holder.rlItemLvDemandAvtivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DemandActivity.class);
                intent.putExtra("ID",object.getId());
                context.startActivity(intent);
            }
        });
        holder.btItemLvGonghuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DemandActivity.class);
                intent.putExtra("ID",object.getId());
                context.startActivity(intent);
            }
        });
        holder.tvItemLvQgTitle.setText(object.getTitle());
        holder.tvItemLvQgGuige.setText(object.getSize());
        if("面议".equals(object.getWant_price().toString())){
            holder.tvItemLvQgSize.setText("面议");
        }else {
            holder.tvItemLvQgSize.setText("￥"+object.getWant_price()+"元/"+object.getUnit());
        }
        holder.tvGhddLvQgGuige.setText(object.getDepot_province_name()+" | "+object.getDepot_city_name());
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_lv_qg_title)
        TextView tvItemLvQgTitle;
        @Bind(R.id.tv_item_lv_guige)
        TextView tvItemLvGuige;
        @Bind(R.id.tv_item_lv_qg_guige)
        TextView tvItemLvQgGuige;
        @Bind(R.id.tv_item_lv_qg_size)
        TextView tvItemLvQgSize;
        @Bind(R.id.bt_item_lv_gonghuo)
        Button btItemLvGonghuo;
        @Bind(R.id.tv_ghdd_lv_qg_guige)
        TextView tvGhddLvQgGuige;
        @Bind(R.id.rl_item_lv_demand_activity)
        RelativeLayout rlItemLvDemandAvtivity;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
